package pcloud.client.util;

import java.io.File;

public class FileResourceInfo implements java.io.Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 215124042765490081L;

	public static String getPathWithoutFileName(String fullPath)
	{
		int lastIndexOfSplitter = fullPath.lastIndexOf('/');
		if (lastIndexOfSplitter < 0) {
			return fullPath;
		}

		return fullPath.substring(0, lastIndexOfSplitter + 1);
	}

	private String absoluteFullPath;

	private String CRC;

	private long creationDate;

	private String extName;

	private String filename;

	private boolean isFolder = false;

	private boolean isRemote = false;

	private long lastModified;

	private long length;

	private String path;

	private String relativeFullPath;

	private String rootPath;

	public FileResourceInfo()
	{

	}

	public FileResourceInfo(File targetFile)
	{
		this.setFilename(targetFile.getName());
		if (targetFile.isDirectory()) {
			this.setFolder(true);
		}
		this.setFullPath(convertPath(targetFile.getPath()));

		this.setLength(targetFile.length());
		this.setLastModified(targetFile.lastModified());
	}

	public FileResourceInfo(File targetFile, String rootPath)
	{
		this(targetFile);
		this.rootPath = rootPath;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#getCRC()
	 */
	public String getCRC()
	{
		return CRC;
	}

	public long getCreationDate()
	{
		return this.creationDate;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#getExtName()
	 */
	public String getExtName()
	{
		return extName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#getFilename()
	 */
	public String getFilename()
	{
		if (filename == null && absoluteFullPath != null) {
			filename = getFileNameFromPath(absoluteFullPath);
		}
		return filename;
	}

	private String getFileNameFromPath(String path)
	{
		int lastIndexOfSplitter = path.lastIndexOf('/');
		if (lastIndexOfSplitter < 0) {
			return path;
		}

		return path.substring(lastIndexOfSplitter + 1);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#getFullPath()
	 */
	public String getFullPath()
	{
		if (absoluteFullPath == null && relativeFullPath != null
				&& rootPath != null) {
			absoluteFullPath = rootPath + (rootPath.endsWith("/") ? "" : "/")
					+ relativeFullPath;
		}
		return absoluteFullPath;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#getLastModified()
	 */
	public long getLastModified()
	{
		return lastModified;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#getLength()
	 */
	public long getLength()
	{
		return length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#getPath()
	 */
	public String getPath()
	{
		return path;
	}

	public String getRelativePath()
	{
		if (relativeFullPath == null) {
			relativeFullPath = absoluteFullPath == null ? ""
								: rootPath == null ? absoluteFullPath 
								: rootPath.equals(absoluteFullPath) ? "" 
								: absoluteFullPath.substring(rootPath.length() + 1);
		}
		return relativeFullPath;
	}

	public String getRootPath()
	{
		if (rootPath==null && absoluteFullPath!=null) {
			GlobalConfig globalConfig = GlobalConfig.getInstance();
			rootPath = this.isRemote ? 
						globalConfig.findRemoteSyncRoot(absoluteFullPath)
						: globalConfig.findLocalSyncRoot(absoluteFullPath);
		}
		return rootPath;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#isFolder()
	 */
	public boolean isFolder()
	{
		return isFolder;
	}

	public boolean isRemote()
	{
		return isRemote;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#lastModified()
	 */
	public long lastModified()
	{
		return getLastModified();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setCRC(java.lang.String)
	 */
	public void setCRC(String crc)
	{
		CRC = crc;
	}

	public void setCreationDate(long creationDate)
	{
		this.creationDate = creationDate;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setExtName(java.lang.String)
	 */
	public void setExtName(String extName)
	{
		this.extName = extName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setFilename(java.lang.String)
	 */
	public void setFilename(String filename)
	{
		this.filename = filename;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setFolder()
	 */
	public void setFolder(boolean isFolder)
	{
		this.isFolder = isFolder;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setFullPath(java.lang.String)
	 */
	public void setFullPath(String fullPath)
	{
		this.absoluteFullPath = fullPath;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setLastModified(long)
	 */
	public void setLastModified(long lastModified)
	{
		this.lastModified = lastModified;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setLength(long)
	 */
	public void setLength(long length)
	{
		this.length = length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pcloud.client.util.FileResourceInfo#setPath(java.lang.String)
	 */
	public void setPath(String path)
	{
		this.path = path;
	}

	public void setRelativePath(String relativeFullPath)
	{
		this.relativeFullPath = relativeFullPath;
	}

	public void setRemote(boolean isRemote)
	{
		this.isRemote = isRemote;
	}

	public void setRootPath(String rootPath)
	{
		this.rootPath = rootPath;
	}

	public String toString()
	{
		return "{FullPath: " + getFullPath() + "\r\n" + "Path: " + getPath()
				+ "\r\n" + "RootPath: " + getRootPath() + "\r\n"
				+ "RelativePath: " + getRelativePath() + "\r\n" + "isRemote: "
				+ isRemote + "\r\n" + "isFolder: " + isFolder() + "\r\n"
				+ "FileName: " + getFilename() + "\r\n" + "FileLength: "
				+ getLength() + "\r\n" + "LastModified: " + getLastModified()
				+ "\r\n" + "CreationDate: " + getCreationDate() + "}";
	}

	private String convertPath(String path)
	{
		return path.replace('\\', '/');
	}

	public FileResourceInfo toRemote()
	{
		String localRootPath = this.getRootPath();
		if (localRootPath == null) {
			return null;
		}
		
		String remoteRootPath = GlobalConfig.getInstance().getRemoteSyncFolder(
				localRootPath);
		if (remoteRootPath == null) {
			return null;
		}
		
		FileResourceInfo remoteInfo = new FileResourceInfo();
		remoteInfo.setRemote(true);
		remoteInfo.setRootPath(remoteRootPath);
		remoteInfo.setFolder(isFolder());
		remoteInfo.setRelativePath(getRelativePath());
		return remoteInfo;
	}

	public FileResourceInfo toLocal()
	{
		String remoteRootPath= this.getRootPath();
		String test = ""; 
		if (remoteRootPath == null) {
			return null;
		}
		
		String localRootPath = GlobalConfig.getInstance().getLocalSyncFolder(
				remoteRootPath);
		if (localRootPath == null) {
			return null;
		}
		
		FileResourceInfo localInfo = new FileResourceInfo();
		localInfo.setRemote(false);
		localInfo.setRootPath(localRootPath);
		localInfo.setFolder(isFolder());
		localInfo.setRelativePath(getRelativePath());
		return localInfo;
	}
}
